package com.lefu.es.system;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.lefu.es.db2.MacDao;
import com.lefu.es.entity.DeviceMacDao;
import com.lefu.es.service.ExitApplication;
import com.lefu.es.util.LogUtil;
import com.wellness.scale.R;

import java.util.List;

/**
 * 绑定秤
 */
public class BindScaleActivity extends AppCompatActivity {

    private static final String TAG = "BindScaleActivity";
    private final static String DEVICE_TEPE_1 = "1";
    private final static String DEVICE_TEPE_2 = "2";
    private final static String DEVICE_TEPE_3 = "3";
    private final static String DEVICE_TEPE_4 = "4";
    private RelativeLayout iv_bind_new;
    private TextView tv_title;
    private LinearLayout layout_back;
    private MyBleAdapter myBleListAdapter;
    private RecyclerView rcv;
    private TextView mTvHint;
    private List<DeviceMacDao> deviceMacs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bind_scale);
        findViews();
        initData();
        initEvent();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    protected void findViews() {
        iv_bind_new = (RelativeLayout) findViewById(R.id.iv_bind_new);
        tv_title = (TextView) findViewById(R.id.tv_title);
        layout_back = (LinearLayout) findViewById(R.id.layout_back);
        rcv = (RecyclerView) findViewById(R.id.rcv);
        mTvHint = (TextView) findViewById(R.id.tv_hint);
        tv_title.setText(R.string.dangqian_shebei);
    }

    private void initData() {
        ExitApplication.getInstance().addActivity(this);
        deviceMacs = MacDao.queryAll();
        myBleListAdapter = new MyBleAdapter();
        rcv.setAdapter(myBleListAdapter);
        if (!deviceMacs.isEmpty()) {
            Log.d(TAG, "deviceMacs:" + deviceMacs.size());
            myBleListAdapter.addData(deviceMacs);
            if (mTvHint.getVisibility() == View.VISIBLE) {
                mTvHint.setVisibility(View.GONE);
            }
        } else {
            if (mTvHint.getVisibility() == View.GONE) {
                mTvHint.setVisibility(View.VISIBLE);
            }
        }
    }

    /***点击事件***/
    private void initEvent() {
        myBleListAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.tvUnbind:
                        unBindBle(position);
                        break;
                    default:
                }
            }
        });
        iv_bind_new.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bindBle();
            }
        });
        layout_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    /*****解除蓝牙绑定******/
    private void unBindBle(final int position) {
        if (!deviceMacs.isEmpty()) {
            MacDao.deleteMac(deviceMacs.get(position).getId());
            myBleListAdapter.remove(position);
            if (mTvHint.getVisibility() == View.VISIBLE) {
                mTvHint.setVisibility(View.GONE);
            }
            query();
        }
    }

    /***绑定设备***/
    private void bindBle() {
        //绑定新设备
        Intent intent = new Intent(BindScaleActivity.this, AutoBLEActivity.class);
        intent.putExtra("flag_from", 1);
        startActivity(intent);
    }

    /***查询数据***/
    private void query() {
        deviceMacs = MacDao.queryAll();
        if (deviceMacs.isEmpty()) {
            if (mTvHint.getVisibility() == View.GONE) {
                mTvHint.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    /*****已绑定绑定蓝牙列表******/
    private class MyBleAdapter extends BaseQuickAdapter<DeviceMacDao, BaseViewHolder> {

        public MyBleAdapter() {
            super(R.layout.item_mac_rcv);
        }

        @Override
        protected void convert(BaseViewHolder helper, DeviceMacDao item) {
            helper.addOnClickListener(R.id.tvUnbind);
            LogUtil.e("*****当前MAC地址-->" + item.getMac() + "****秤的类型-->"+item.getDeviceType());
            switch (item.getDeviceType()) {
                case DEVICE_TEPE_1:
                    helper.setText(R.id.tvMacAndName, getString(R.string.mac_ce_show) + "\n" + item.getMac());
                    break;
                case DEVICE_TEPE_2:
                    helper.setText(R.id.tvMacAndName, getString(R.string.mac_cf_show) + "\n" + item.getMac());
                    break;
                case DEVICE_TEPE_3:
                    helper.setText(R.id.tvMacAndName, getString(R.string.mac_ca_show) + "\n" + item.getMac());
                    break;
                case DEVICE_TEPE_4:
                    helper.setText(R.id.tvMacAndName, getString(R.string.mac_cb_show) + "\n" + item.getMac());
                    break;
                default:
                    helper.setText(R.id.tvMacAndName, getString(R.string.mac_cf_show) + "\n" + item.getMac());
                    break;
            }
        }
    }
}
